package com.xingda.packing.dmss.rule.choose;

import com.xingda.packing.dmss.domain.ProductBox;
import com.xingda.packing.dmss.domain.Wheel;
import com.xingda.packing.dmss.enums.BoxTaskType;
import com.xingda.packing.dmss.enums.WheelFinishedState;
import com.xingda.packing.dmss.enums.WheelQualityState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 质量状态
 *
 * @author LEGION
 */
@Order(3)
@Component
@Slf4j
public class QualityChoose implements ChooseService {

    @Override
    public boolean canChoose(ProductBox productBox, Wheel wheel) {
        boolean result = can(productBox, wheel);
        if (!result) {
            log.info("box:{}, 工字轮条码：{}，质量->{}", productBox.getBoxNo(), wheel.getBarCode(), false);
        }
        return result;
    }

    private boolean can(ProductBox productBox, Wheel wheel) {
        if (Objects.nonNull(wheel)) {
            if (Objects.nonNull(wheel.getQualityState())) {
                // 装箱任务和清线任务质量合格并且编码XC开头(有质量合格的成品轮需要去重卷，贴的XD开头的码，不进行成品装箱)
                if (wheel.isAvailable()
                        && (productBox.getType() == BoxTaskType.PACKING_FINISHED.getTypeId()
                        || productBox.getType() == BoxTaskType.PACKING_CLEAN.getTypeId())) {
                    return true;
                }
                // 不可用产品(半成品和不合格产品)
                return (wheel.getQualityState() != WheelQualityState.QUALITY ||
                        wheel.getFinishedState() == WheelFinishedState.SEMI_FINISHED ||
                        wheel.getBarCode().startsWith("XD"))
                        && productBox.getType() == BoxTaskType.PACKING_SEMI_FINISHED_DISQUALITY.getTypeId();
            }
        }
        return false;
    }
}
